package com.ruoyi.framework.utils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class StringUtil {

	/**
	 * 字符串非空处理
	 * @param val
	 * @return
	 */
	public static String getTrim(Object val) {
		if (val == null || val.equals("null") || val.equals("undefined")) {
			val = "";
		}
		return val.toString().trim();
	}

	/**
	 * 返回数据库模糊查询的参数值
	 * @param value
	 * @return
	 */
	public static String getQueryParam(Object value){
		String val = getTrim(value,"%");
		if(val.equals("%")){
			return val;
		}else{
			return "%"+val+"%";
		}
	}

	/**
	 * 获得非空字符串，当为空时，可以设置为额外的字符
	 * @param value 字符串
	 * @param fill 填充值
	 * @return
	 */
	public static String getTrim(Object value,String fill){
		if (value == null || value.equals("null") || value.equals("undefined") || value.equals("")) {
			value = fill;
		}
		return value.toString().trim();
	}

	/**
	 * 判断字符串是否为空，空包含：""、null、"null"、"undefined"
	 * @param value
	 * @return
	 */
	public static boolean isEmpty(Object value){
		value = getTrim(value);
		return value.equals("");
	}

	public static int toInt(Object obj){
		return (int) toDouble(obj);
	}
	
	public static long toLong(Object obj){
		String str = getTrim(obj);
		if(str.equals(""))
			return 0;
		try{
			return Long.parseLong(str);
		}catch(Exception ex){
			return 0;
		}
	}
	
	public static float toFloat(Object obj){
		String str = getTrim(obj);
		if(str.equals(""))
			return 0;
		try{
			return Float.parseFloat(str);
		}catch(Exception ex){
			return 0;
		}
	}

	public static double toDouble(Object obj){
		String str = getTrim(obj);
		if(str.equals(""))
			return 0;
		try{
			return Double.parseDouble(str);
		}catch(Exception ex){
			return 0;
		}
	}
	
	/**
	 * 对比两个对符串的值，如果相等返回true，否则返回false
	 * @param obj1 需要对比的值1
	 * @param obj2 需要对比的值2
	 * @return 如果相等返回true，否则返回false
	 */
	public static boolean equals(Object obj1,Object obj2){
		if(obj1 == null){
			// log.error("obj1 值为null!");
			return false;
		}
		
		if(obj2 == null){
			// log.error("obj2值为null!");
			return false;
		}
		
		if(obj1.toString().equals(obj2.toString())){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 对比两个对符串的值，如果相等返回true，否则返回false
	 * @param obj1 需要对比的值1
	 * @param obj2 需要对比的值2
	 * @param obj2 是否允许为Null
	 * @return 如果相等返回true，否则返回false
	 */
	public static boolean equals(Object obj1,Object obj2,boolean allowNull){
		if(allowNull && obj1 == null && obj2 == null){
			return true;
		}

		if(obj1 == null){
			// log.error("obj1 值为null!");
			return false;
		}

		if(obj2 == null){
			// log.error("obj2值为null!");
			return false;
		}

		if(obj1.toString().equals(obj2.toString())){
			return true;
		}else{
			return false;
		}
	}

	public static String[] getUniqueCode(String str) {
		String key = "hsh-framework";
		String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

		String sMD5EncryptResult = MD5Util.MD5(key + str);
		String hex = sMD5EncryptResult;
		String[] resUrl = new String[4];

		for(int i = 0; i < 4; ++i) {
			String sTempSubString = hex.substring(i * 8, i * 8 + 8);
			long lHexLong = 1073741823L & Long.parseLong(sTempSubString, 16);
			String outChars = "";

			for(int j = 0; j < 6; ++j) {
				long index = 61L & lHexLong;
				outChars = outChars + chars[(int)index];
				lHexLong >>= 5;
			}

			resUrl[i] = outChars;
		}

		return resUrl;
	}

	/**
	 * toString放置空指针异常
	 * @param str
	 * @return
	 */
	public static String toString(Object str){
		return null == str ? null : str.toString();
	}


	public static String rightPad(final String str, final int size, final char padChar) {
		if (str == null) {
			return null;
		}
		final int pads = size - str.length();
		if (pads <= 0) {
			return str; // returns original String when possible
		}
		return str.concat(repeat(padChar, pads));
	}
	public static String leftPad(final String str, final int size, final char padChar) {
		if (str == null) {
			return null;
		}
		final int pads = size - str.length();
		if (pads <= 0) {
			return str; // returns original String when possible
		}
		return repeat(padChar, pads).concat(str);
	}

	public static String repeat(final char ch, final int repeat) {
		if (repeat <= 0) {
			return "";
		}
		final char[] buf = new char[repeat];
		for (int i = repeat - 1; i >= 0; i--) {
			buf[i] = ch;
		}
		return new String(buf);
	}
}
